package abc.def.ui;

import abc.def.SimpleHelper;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;

import javax.swing.*;

public class CardRewardDialog extends JDialog {
    final static private String CONTENT_COLOR = "000000";
    private JPanel contentPane;
    private JComboBox allCardComboBox;
    private JButton refreshButton;
    private JButton changeButton;

    public CardRewardDialog() {
        setTitle("Card Reward");
        setContentPane(contentPane);
        setModal(false);
        setDefaultCloseOperation(HIDE_ON_CLOSE);

        refreshButton.addActionListener(e -> {
            allCardComboBox.removeAllItems();
            for (AbstractCard card : AbstractDungeon.commonCardPool.group) {
                allCardComboBox.addItem(cardToHtml(card, "999999"));
            }
            for (AbstractCard card : AbstractDungeon.uncommonCardPool.group) {
                allCardComboBox.addItem(cardToHtml(card, "0000CC"));
            }
            for (AbstractCard card : AbstractDungeon.rareCardPool.group) {
                allCardComboBox.addItem(cardToHtml(card, "FF9900"));
            }
            pack();
        });

        changeButton.addActionListener(e -> {
            int index = allCardComboBox.getSelectedIndex();
            if (index >= 0) {
                int[] sizes = new int[]{AbstractDungeon.commonCardPool.size(), AbstractDungeon.uncommonCardPool.size(), AbstractDungeon.rareCardPool.size()};
                if (index < sizes[0]) {
                    SimpleHelper.toChangeCard = AbstractDungeon.commonCardPool.group.get(index).makeCopy();
                } else if (index < sizes[0] + sizes[1]) {
                    SimpleHelper.toChangeCard = AbstractDungeon.uncommonCardPool.group.get(index - sizes[0]).makeCopy();
                } else if (index < sizes[0] + sizes[1] + sizes[2]) {
                    SimpleHelper.toChangeCard = AbstractDungeon.rareCardPool.group.get(index - sizes[0] - sizes[1]).makeCopy();
                }
            }
        });
    }

    private String cardToHtml(AbstractCard card, String titleColor) {
        return String.format("<html><span style='color:#%s'>%s<span/><br/><span style='color:#%s'>%s<span/><html/>", titleColor, card.name, CONTENT_COLOR, card.rawDescription);
    }

    public static void main(String[] args) {
        CardRewardDialog dialog = new CardRewardDialog();
        dialog.pack();
        dialog.setVisible(true);
    }
}
